﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using control.Biz;

public partial class usercontrol_details : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataList1.ItemCommand += new DataListCommandEventHandler(DataList1_ItemCommand);
        if (!IsPostBack)
        {
            if (Request.QueryString["IDSach"] != null)
            {
                int _ID = int.Parse(Request.QueryString["IDSach"].ToString());
                BizSach sach = BizSach.GetSachByID(_ID);
                List<BizSach> saches = new List<BizSach>();
                saches.Add(sach);
                DataList1.DataSource = saches;
                DataList1.DataBind();
                
            }
        }   
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        bool kt = false;
        int ID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        BizSach sach = BizSach.GetSachByID(ID);
        BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
        int indexItem = cart.FindIndexItem(ID);
        if (indexItem == -1)
        {
            BizChiTietPhieuXuat cartItem = new BizChiTietPhieuXuat()
            {
                Sach = sach,
                SoLuong = 1,
            };
            cart.Items.Add(cartItem);
        }
        else
        {
            cart.Items[indexItem].SoLuong += 1;
        }
        Session["cart"] = cart;
        Response.Redirect("Cart.aspx");

    }


}
